package dwz.web;

import java.util.Date;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.support.RequestContextUtils;

import dwz.common.util.web.PageInfo;
import dwz.web.editor.DateEditor;
import dwz.web.editor.DoubleEditor;
import dwz.web.editor.IntegerEditor;
import dwz.web.editor.LongEditor;

public abstract class BaseController {
	/**
	 * The paging size parameter
	 */
	protected static final String PAGING_PARAM_PAGESIZE = "numPerPage";
	
	/**
	 * The paging number parameter
	 */
	protected static final String PAGING_PARAM_PAGENUM = "pageNum";
	@Autowired
	protected ResourceBundleMessageSource _res;
	
//	@Autowired
//	protected SpringContextHolder _contextHolder;

	@InitBinder
	protected void initBinder(HttpServletRequest request,
			ServletRequestDataBinder binder) throws Exception {
		
		binder.registerCustomEditor(int.class, new IntegerEditor());
		binder.registerCustomEditor(long.class, new LongEditor());
		binder.registerCustomEditor(double.class, new DoubleEditor());
		binder.registerCustomEditor(Date.class, new DateEditor());
	}
	
	protected ModelAndView ajaxDone(int statusCode, String message) {
		ModelAndView mav = new ModelAndView("ajaxDone");
		mav.addObject("statusCode", statusCode);
		mav.addObject("message", message);
		return mav;
	}

	protected ModelAndView ajaxDoneSuccess(String message) {
		return ajaxDone(200, message);
	}

	protected ModelAndView ajaxDoneError(String message) {
		return ajaxDone(200, message);
	}
	protected String getMessage(String code) {
		return this.getMessage(code, new Object[] {});
	}

	protected String getMessage(String code, Object arg0) {
		return getMessage(code, new Object[] { arg0 });
	}

	protected String getMessage(String code, Object arg0, Object arg1) {
		return getMessage(code, new Object[] { arg0, arg1 });
	}

	protected String getMessage(String code, Object arg0, Object arg1,
			Object arg2) {
		return getMessage(code, new Object[] { arg0, arg1, arg2 });
	}

	protected String getMessage(String code, Object arg0, Object arg1,
			Object arg2, Object arg3) {
		return getMessage(code, new Object[] { arg0, arg1, arg2, arg3 });
	}

	protected String getMessage(String code, Object[] args) {
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
		Locale locale = localeResolver.resolveLocale(request);

		return _res.getMessage(code, args, locale);
	}
	
	/**
	 * Obtains the paging bean from request
	 * 
	 * @param request
	 * @return
	 */
	protected PageInfo getPagingBean (HttpServletRequest request)
	{
		try
		{
			return new PageInfo(
					request.getParameter(PAGING_PARAM_PAGENUM) != null ? 
							Integer.parseInt(request.getParameter(PAGING_PARAM_PAGENUM)) : 1, 
					request.getParameter(PAGING_PARAM_PAGESIZE) != null ? Integer.parseInt(
							request.getParameter(PAGING_PARAM_PAGESIZE)) : PageInfo.DEFAULT_PAGE_SIZE);
		}
		catch (Exception e)
		{
			return new PageInfo(0, PageInfo.DEFAULT_PAGE_SIZE);
		}
	}
	
	protected ModelAndView uploadDone(String data) {
		ModelAndView mav = new ModelAndView("uploadDone");
		mav.addObject("statusCode", "1");
		mav.addObject("data", data);
		return mav;
	}
	
	protected ModelAndView uploadFailed() {
		ModelAndView mav = new ModelAndView("uploadDone");
		mav.addObject("statusCode", "0");
		mav.addObject("message", "upload file failed!");
		return mav;
	}
}
